# -*- coding: utf-8 -*-
#
# Copyright (c) 2012, Rafael Pivato
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of Rafael Pivato nor the names of its contributors
#       may be used to endorse or promote products derived from this
#       software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL RAFAEL PIVATO BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from google.appengine.ext import db
from google.appengine.api import memcache


class WowIcon(db.Model):
    """Represents a character in our database."""

    name = db.StringProperty(required=True)
    icon = db.StringProperty(required=True)

    def wow_id(self):
        """Returns the id for this Wow element."""
        return self.key().id()

    def put(self):
        """saves this object in cache after putting it on the datastore."""
        db.Model.put(self)
        str_id = str(self.key().id())
        memcache.set(str_id, self, namespace=self._namespace())

    @classmethod
    def key_for_wow_id(cls, wow_id):
        """Returns a database Key object for the given id and this class."""
        return db.Key.from_path(cls.__name__, wow_id)

    @classmethod
    def _namespace(cls):
        """Returns the namespace for this class."""
        return '%s.%s' % (cls.__module__, cls.__name__)

    @classmethod
    def get_by_wow_id(cls, wow_id):
        """Returns the icon from the cache or datastore."""
        str_id = str(wow_id)
        cached = memcache.get(str_id, cls._namespace())
        if cached is None:
            key = cls.key_for_wow_id(wow_id)
            entity = cls.get(key)
            if not entity is None:
                memcache.set(str_id, entity, namespace=cls._namespace())
            return entity
        else:
            return cached

    @classmethod
    def set_by_wow_id(cls, wow_id, **kwds):
        """Saves a new icon under the given id."""
        entity = cls(key=cls.key_for_wow_id(wow_id), **kwds)
        entity.put()
        return entity

    @classmethod
    def cache_invalid_id(cls, wow_id):
        """Marks the given id as invalid."""
        wow_id = str(wow_id)
        memcache.set(wow_id, False, 604800, namespace=cls._namespace())


class WowItemIcon(WowIcon):
    """Represents an item icon in our datastore."""

    description = db.StringProperty(default='')
    quality = db.IntegerProperty(required=True, default=1)


class WowSpellIcon(WowIcon):
    """Represents a spell icon in our datastore."""

    pass
